# Replication code for Taylor C. Boas (Forthcoming), "Pastors for Pinochet: Authoritarian Stereotypes and Voting for Evangelicals in Chile," Journal of Experimental Political Science. This article analyzes the 2013 survey "Religion and Gender in Chile's 2013 Elections." The dataset included here contains only those variables necessary to replicate the analysis. The full dataset will be archived at the Harvard Dataverse Network, http://thedata.harvard.edu/dvn/, at a later date.

rm(list=ls(all=T))

# Set working directory as appropriate

setwd('~/Dropbox/Current Research/Skeletons Under the Altar/jeps_replication/')

# Load packages

library(Matching)
library(Hmisc)
library(car)
library(foreign)

# Load replication file. It contains the following objects:
#
# survey: selected variables from the "Religion and Gender in Chile's 2013 Elections" survey.
# reg.age, religion, comunas, education, age: Data from Chile's 2012 census (preliminary report, at http://www.emol.com/documentos/archivos/2013/04/02/20130402145438.pdf), used in checking representativeness of the survey.

load('pastors_pinochet_replication.RData')

# ========================================
# Set up survey file, create new variables
# ========================================

survey$treat.pinochet<-survey$p9_treat==1
survey$treat.evang<-survey$p10_treat %in% 1|survey$p10_real_treat %in% 1
survey$treat.evang[is.na(survey$p10_treat)&is.na(survey$p10_real_treat)]<-NA

survey$real.cand<-!is.na(survey$p10_real)
survey$real.cand[is.na(survey$p10_real)&is.na(survey$p10)]<-NA
survey$real.cand.comuna<-survey$comuna %in% survey$comuna[which(survey$real.cand)]

survey$evang.cand.vote<-ifelse(is.na(survey$p10),survey$p10_real,survey$p10)
survey$evang.cand.vote[survey$evang.cand.vote==-99]<-NA

survey$catholic<-survey$p23==1
survey$evangelical<-survey$p23==2
survey$none<-survey$p23==4|survey$p23==5
survey$other<-survey$p23==6|survey$p23==3

survey$ideol<-ifelse(survey$p6>0,survey$p6,NA)
survey$tendency<-1*(survey$ideol %in% 1:4) + 2*(survey$ideol %in% 5:6) + 3*(survey$ideol %in% 7:10)
survey$tendency[survey$tendency==0]<-NA
survey$tendency<-factor(survey$tendency,labels=c('Leftist','Centrist','Rightist'))

survey$noparty<-survey$p26==2
survey$noparty[survey$p26==-99]<-NA
survey$PS<-survey$p27 %in% 15
survey$PS[is.na(survey$p26)|survey$p26==-99]<-NA
survey$PPD<-survey$p27 %in% 11
survey$PPD[is.na(survey$p26)|survey$p26==-99]<-NA
survey$PDC<-survey$p27 %in% 3
survey$PDC[is.na(survey$p26)|survey$p26==-99]<-NA
survey$RN<-survey$p27 %in% 16
survey$RN[is.na(survey$p26)|survey$p26==-99]<-NA
survey$UDI<-survey$p27 %in% 17
survey$UDI[is.na(survey$p26)|survey$p26==-99]<-NA
survey$PC<-survey$p27 %in% 2
survey$PC[is.na(survey$p26)|survey$p26==-99]<-NA
survey$otherparty<-survey$p27 %in% c(1,4,7,8,9,10,12,13,14)
survey$otherparty[is.na(survey$p26)|survey$p26==-99]<-NA

survey$screener1.pass<-survey$p7_k
survey$screener2.pass<-survey$p20_10==1&survey$p20_5==1&survey$p20_1==0&survey$p20_2==0&survey$p20_3==0&survey$p20_4==0&survey$p20_6==0&survey$p20_7==0&survey$p20_8==0&survey$p20_9==0
survey$screener<-factor(apply(survey[,c('screener1.pass','screener2.pass')],1,sum,na.rm=T),labels=c('None','One','Both'))

survey$edu<-ifelse(survey$p32>0,survey$p32,NA)
survey$age<-survey$p3
survey$interest<-ifelse(survey$p5>0,survey$p5,NA)
survey$male<-ifelse(survey$p31== -99, NA, survey$p31==1)

survey$church.rev<-ifelse(survey$p25== -99,NA, -1*survey$p25 + 5)

#############
# NOTE: Code to reproduce Figure 1 from the main text is at the end of this file. The code to generate Appendix Tables 10, 11, and 12 must be run first (as it will be if you run this whole file).
#############

# =======================
# Appendix Tables 2 and 3
# =======================

region.pop<-sapply(reg.age,function(x) sum(x$total.both[19:106])) # Gives regional totals for ages 18+
region.pct.pop<-100*prop.table(region.pop)
names(region.pct.pop)<-c('Arica y Parinacota',"Tarapaca",'Antofagasta','Atacama','Coquimbo',"Valparaiso",'Metropolitana',"O'Higgins",'Maule',"Biobio","Araucania","Los Rios",'Los Lagos',"Aysen",'Magallanes y Antartica')
region.pct.sample.notarg<-100*prop.table(table(survey$region[survey$targ==0]))
names(region.pct.sample.notarg)<-c("Tarapaca",'Antofagasta','Atacama','Coquimbo',"Araucania",'Metropolitana',"Valparaiso","O'Higgins",'Maule',"Biobio",'Los Lagos',"Aysen",'Magallanes y Antartica',"Los Rios",'Arica y Parinacota')
region.pct.sample<-100*prop.table(table(survey$region[!survey$real.cand]))
names(region.pct.sample)<-c("Tarapaca",'Antofagasta','Atacama','Coquimbo',"Araucania",'Metropolitana',"Valparaiso","O'Higgins",'Maule',"Biobio",'Los Lagos',"Aysen",'Magallanes y Antartica',"Los Rios",'Arica y Parinacota')

religion.pop<-data.frame(Catholic=religion$catholic[1],Evangelical=religion$evangelical[1],Other=sum(religion[1,c('jewish','muslim','mormon','orthodox','buddhist','bahai','jehova','indigenous','other')]),None=religion$none[1])
religion.pct.pop<-100*prop.table(religion.pop)
religion.pct.sample<-with(survey[!survey$real.cand,],100*prop.table(data.frame(Catholic=sum(catholic,na.rm=T),Evangelical=sum(evangelical,na.rm=T),Other=sum(other,na.rm=T),None=sum(none,na.rm=T))))
religion.pct.sample.notarg<-with(survey[survey$targ==0,],100*prop.table(data.frame(Catholic=sum(catholic,na.rm=T),Evangelical=sum(evangelical,na.rm=T),Other=sum(other,na.rm=T),None=sum(none,na.rm=T))))

age.pop<-data.frame(age=18:105,total=age$total.both[19:106])
age.pop$pct<-100*prop.table(age.pop$total)
age.pop$cum.pct<-cumsum(age.pop$pct)
age.med.pop<-age.pop$age[which(age.pop$cum.pct==min(age.pop$cum.pct[age.pop$cum.pct>50]))]
age.med.sample<-median(survey$p3[!survey$real.cand],na.rm=T)
age.med.sample.notarg<-median(survey$p3[survey$targ==0],na.rm=T)

male.pct.pop<-100*sum(age$total.men[19:106])/sum(age$total.both[19:106])
male.pct.sample<-100* mean(survey$male[!survey$real.cand],na.rm=T)
male.pct.sample.notarg<-100* mean(survey$male[survey$targ==0],na.rm=T)

edu.pop<-apply(education[education$level %in% c('Nunca asistió','Jardín infantil/Sala cuna ','Kínder/Pre-Kínder','Diferencial','Básica, Primariao Preparatoria','Media Científico Humanista o Humanidades','Media Técnico Profesional, Comercial, Industrial o Normalista ','Técnico de Nivel Superior ','Profesional','Postítulo ','Magíster','Doctorado'),c('age15to19','age20to24','age25to29','age30to39','age40to49','age50plus')],1,sum)
names(edu.pop)<-c('Nunca asistió','Jardín infantil/Sala cuna ','Kínder/Pre-Kínder','Diferencial','Básica, Primariao Preparatoria','Media Científico Humanista o Humanidades','Media Técnico Profesional, Comercial, Industrial o Normalista ','Técnico de Nivel Superior ','Profesional','Postítulo ','Magíster','Doctorado')
edu.pop<-data.frame(None=sum(edu.pop[c('Nunca asistió','Jardín infantil/Sala cuna ','Kínder/Pre-Kínder','Diferencial')]),Primary=edu.pop['Básica, Primariao Preparatoria'],Secondary=sum(edu.pop[c('Media Científico Humanista o Humanidades','Media Técnico Profesional, Comercial, Industrial o Normalista ')]),Technical=edu.pop['Técnico de Nivel Superior '],University=edu.pop['Profesional'],Postgraduate=sum(edu.pop[c('Postítulo ','Magíster','Doctorado')]))
edu.pct.pop<-100*prop.table(edu.pop)
survey$edu.cat<-recode(survey$p32,"1='None';2:3='Primary';4:5='Secondary';6:7='Technical';8:9='University';10='Postgraduate';else=NA")
edu.pct.sample<-100*prop.table(table(survey$edu.cat[!survey$real.cand]))
edu.pct.sample<-edu.pct.sample[names(edu.pct.pop)]
edu.pct.sample.notarg<-100*prop.table(table(survey$edu.cat[survey$targ==0]))
edu.pct.sample.notarg<-edu.pct.sample.notarg[names(edu.pct.pop)]

comunas<-comunas[order(comunas$total),]
comunas$pct<-100*comunas$total/sum(comunas$total)
comunas$cum.pct<-cumsum(comunas$pct)
comuna.med.pop<-comunas$total[which(comunas$cum.pct==min(comunas$cum.pct[comunas$cum.pct>50]))]
comunas$area[comunas$area=='Los Álamos']<-'Los Alamos'
comunas$area[comunas$area=='Lampa ']<-'Lampa'
comunas$area[comunas$area=='Coyhaique']<-'Coihaique'
comunas$area[comunas$area=='Aysén']<-'Aisén'
comunas$area[comunas$area=='Calera']<-'La Calera'
comunas$area[comunas$area=='Nacimiento ']<-'Nacimiento'
comunas$area[comunas$area=='Llaillay ']<-'Llaillay'
survey$comuna.pop<-comunas$total[match(survey$comuna, comunas$area)]
comuna.med.sample<-median(survey$comuna.pop[!survey$real.cand],na.rm=T)
comuna.med.sample.notarg<-median(survey$comuna.pop[survey$targ==0],na.rm=T)

# Download the Chile 2012 AmericasBarometer survey (Stata version) from http://www.vanderbilt.edu/lapop/free-access.php and load it, modifying the code below as necessary for the filename and location. If the filename differs from that below, you may be working with a newer version of the file, which could potentially affect the results or require changes to the code.

lapop12<-read.dta('420502678Chile LAPOP AmericasBarometer 2012 Rev1_W.dta')

lapop12$church<-lapop12$q5a[,drop=T]
church.pct.pop<-100*prop.table(table(lapop12$church))
survey$church<-ifelse(survey$p25== -99,NA, survey$p25)
church.pct.sample<-100*prop.table(table(survey$church[!survey$real.cand]))
names(church.pct.sample)<-names(church.pct.pop)
church.pct.sample.notarg<-100*prop.table(table(survey$church[survey$targ==0]))
names(church.pct.sample.notarg)<-names(church.pct.pop)

lapop12$noparty<-lapop12$vb10 == 'No'
lapop12$PS<-unclass(lapop12$vb11) %in% 5
lapop12$PS[is.na(lapop12$vb10)]<-NA
lapop12$PPD<-unclass(lapop12$vb11) %in% 6
lapop12$PPD[is.na(lapop12$vb10)]<-NA
lapop12$PDC<-unclass(lapop12$vb11) %in% 7
lapop12$PDC[is.na(lapop12$vb10)]<-NA
lapop12$RN<-unclass(lapop12$vb11) %in% 8
lapop12$RN[is.na(lapop12$vb10)]<-NA
lapop12$UDI<-unclass(lapop12$vb11) %in% 9
lapop12$UDI[is.na(lapop12$vb10)]<-NA
lapop12$PC<-unclass(lapop12$vb11) %in% 11
lapop12$PC[is.na(lapop12$vb10)]<-NA
lapop12$otherparty<-unclass(lapop12$vb11) %in% c(1,10)
lapop12$otherparty[is.na(lapop12$vb10)]<-NA
party.pct.pop<-100*apply(lapop12[,c('noparty','PS','PPD','PDC','RN','UDI','PC','otherparty')],2,mean,na.rm=T)
party.pct.sample<-100*apply(survey[!survey$real.cand,c('noparty','PS','PPD','PDC','RN','UDI','PC','otherparty')],2,mean,na.rm=T)
party.pct.sample.notarg<-100*apply(survey[survey$targ==0,c('noparty','PS','PPD','PDC','RN','UDI','PC','otherparty')],2,mean,na.rm=T)

lapop12$tendency<-recode(unclass(lapop12$l1),"1:4='Leftist';5:6='Centrist';7:10='Rightist'",as.factor.result=T,levels=c('Leftist','Centrist','Rightist'))
ideol.pct.pop<-100*prop.table(table(lapop12$tendency))
ideol.pct.sample<-100*prop.table(table(survey$tendency[!survey$real.cand]))
ideol.pct.sample.notarg<-100*prop.table(table(survey$tendency[survey$targ==0]))

sample.stats<-data.frame(t(round(c(comuna.med.sample,region.pct.sample,unlist(religion.pct.sample),edu.pct.sample,age.med.sample,male.pct.sample,church.pct.sample,party.pct.sample,ideol.pct.sample),1)))
sample.stats.notarg<-data.frame(t(round(c(comuna.med.sample.notarg,region.pct.sample.notarg,unlist(religion.pct.sample.notarg),edu.pct.sample.notarg,age.med.sample.notarg,male.pct.sample.notarg,church.pct.sample.notarg,party.pct.sample.notarg,ideol.pct.sample.notarg),1)))
pop.stats<-data.frame(t(round(c(comuna.med.pop,region.pct.pop,unlist(religion.pct.pop),unlist(edu.pct.pop),age.med.pop,male.pct.pop,church.pct.pop,party.pct.pop,ideol.pct.pop),1)))
rep.table<-data.frame(t(rbind(sample.stats,sample.stats.notarg,pop.stats)))

rep.table.census<-rep.table[1:28,]
rep.table.census<-apply(rep.table.census,2,prettyNum,big.mark=',')
rep.table.lapop<-rep.table[29:44,]
rep.table.lapop<-apply(rep.table.lapop,2,prettyNum,big.mark=',')

bottom.note.census<-c("Individual census figures are for residents 15 and older (religion and education) or 18 and older (other variables). Comuna figures are those associated with the median individual. Non-median figures are percentages. Education is the highest level started or completed. ``Used'' and ``Untargeted'' samples are defined in the text.")
bottom.note.lapop<-c("All figures expressed as percentages of registered voters. ``Used'' and ``Untargeted'' samples are defined in the text.")

rep.table.census.latex<-latex(rep.table.census,file='rep_table_census.tex',caption='Online Sample vs. 2012 Census',rowlabel='',rowname=c('Median Population',"Tarapac\\'a",'Antofagasta','Atacama','Coquimbo',"Araucan\\'ia",'Metropolitana',"Valpara\\'iso","O'Higgins",'Maule',"Biob\\'io",'Los Lagos',"Ays\\'en","Magallanes y Ant\\'artica","Los R\\'ios",'Arica y Parinacota','Catholic','Evangelical','Other','None','None','Primary','Secondary','Technical','University','Postgraduate','Median Age','Male'),colheads=c('Sample\n(Used)','Sample\n(Untargeted)','Census'),extracolsize='normalsize',collabel.just=rep('r',3),col.just = rep('r',3),insert.bottom=bottom.note.census,rgroup = c('Comuna','Region','Religion','Education','Other'), n.rgroup = c(1,15,4,6,2), booktabs = F, ctable = T, where = "htp")

rep.table.lapop.latex<-latex(rep.table.lapop,file='rep_table_lapop.tex',caption='Online Sample vs. 2012 AmericasBarometer',rowlabel='',rowname=c('1+ Times/Week','1 Time/Week','1 Time/Month','1--2 Times/Year','Never/Almost Never','None','PS','PPD','PDC','RN','UDI','PC','Other','Left (1--4)','Center (5--6)','Right (7--10)'),colheads=c('Sample\n(Used)','Sample\n(Untargeted)','Americas\nBarometer'),extracolsize='normalsize',collabel.just=rep('r',3),col.just = rep('r',3),insert.bottom= bottom.note.lapop,rgroup = c('Church Attendance','Party ID','Ideology'), n.rgroup = c(5,8,3), booktabs = F, ctable = T, where = "htp")

# =======================
# Appendix Tables 4 and 5
# =======================

survey$log.comuna.pop<-log(survey$comuna.pop)
survey$reg.num<-sapply(strsplit(survey$region,' - '),function(x) x[1])
survey$north<-survey$reg.num %in% c('I','II','III','IV','V','XV')
survey$RM<-survey$reg.num %in% 'RM'
survey$south<-survey$reg.num %in% c('VI','VII','VIII','IX','X','XI','XII','XIV')
survey$partisan<-!survey$noparty

bal.data<-survey[!survey$real.cand,c('log.comuna.pop','north','RM','south','catholic','evangelical','other','none','church.rev','partisan','ideol','interest','age','edu','male')]

pinochet.mean.treat<-apply(bal.data[which(survey$treat.pinochet),],2,mean,na.rm=T)
pinochet.mean.control<-apply(bal.data[which(!survey$treat.pinochet),],2,mean,na.rm=T)
pinochet.sd<-apply(bal.data,2,sd,na.rm=T)
pinochet.stddiff<- (pinochet.mean.treat - pinochet.mean.control)/pinochet.sd
pinochet.ttest<-apply(bal.data,2,function(x) t.test(x[which(survey$treat.pinochet)],x[which(!survey$treat.pinochet)])$p.value)
pinochet.varrat<-apply(bal.data,2,function(x) var(x[which(survey$treat.pinochet)],na.rm=T)/var(x[which(!survey$treat.pinochet)],na.rm=T))
pinochet.kstest<- apply(bal.data,2,function(x) ifelse(length(table(x))==2,NA,ks.boot(Tr=x[which(survey$treat.pinochet)],Co=x[which(!survey$treat.pinochet)],nboots=1000)$ks.boot.pvalue))

evang.mean.treat<-apply(bal.data[which(survey$treat.evang),],2,mean,na.rm=T)
evang.mean.control<-apply(bal.data[which(!survey$treat.evang),],2,mean,na.rm=T)
evang.sd<-apply(bal.data,2,sd,na.rm=T)
evang.stddiff<- (evang.mean.treat - evang.mean.control)/evang.sd
evang.ttest<-apply(bal.data,2,function(x) t.test(x[which(survey$treat.evang)],x[which(!survey$treat.evang)])$p.value)
evang.varrat<-apply(bal.data,2,function(x) var(x[which(survey$treat.evang)],na.rm=T)/var(x[which(!survey$treat.evang)],na.rm=T))
evang.kstest<- apply(bal.data,2,function(x) ifelse(length(table(x))==2,NA,ks.boot(Tr=x[which(survey$treat.evang)],Co=x[which(!survey$treat.evang)],nboots=1000)$ks.boot.pvalue))

bal.table.pinochet<-round(data.frame(pinochet.mean.treat,pinochet.mean.control,pinochet.stddiff,pinochet.varrat,pinochet.ttest,pinochet.kstest),2)
names(bal.table.pinochet)<-c('Treated','Control','Std.\\ Diff.','Var.\\ Rat.','t-test','KS-test')
rownames(bal.table.pinochet)<-c('Log Population','North','Santiago','South','Catholic','Evangelical','Other','None','Church Attendance (0--4)','Partisan','Ideology (1--10)','Campaign Interest (1--7)','Age','Education (1--10)','Male')

bottom.note<-c("NOTE: `Treated' and `Control' give mean values; `Std.\\ Diff.' is their difference divided by the pooled standard deviation. `Var.\\ Rat.' is the ratio of treatment to control group variance. `t-test' and `KS-test' give two-sided p-values (bootstrapped for KS).") 

bal.table.pinochet.latex<-latex(bal.table.pinochet,file='bal_table_pinochet.tex',rowlabel='',collabel.just=rep('r',6),col.just = rep('r',6), caption = "Covariate Balance for Pinochet Treatment", insert.bottom=bottom.note,rgroup = c('Comuna','Region','Religion','Politics','Demographics'), n.rgroup = c(1,3,5,3,3), booktabs = F, ctable = T, where = "htp",na.blank=T)

bal.table.evang<-round(data.frame(evang.mean.treat,evang.mean.control,evang.stddiff,evang.varrat,evang.ttest,evang.kstest),2)
names(bal.table.evang)<-c('Treated','Control','Std.\\ Diff.','Var.\\ Rat.','t-test','KS-test')
rownames(bal.table.evang)<-c('Log Population','North','Santiago','South','Catholic','Evangelical','Other','None','Church Attendance (0--4)','Partisan','Ideology (1--10)','Campaign Interest (1--7)','Age','Education (1--10)','Male')

bottom.note<-c("NOTE: `Treated' and `Control' give mean values; `Std.\\ Diff.' is their difference divided by the pooled standard deviation. `Var.\\ Rat.' is the ratio of treatment to control group variance. `t-test' and `KS-test' give two-sided p-values (bootstrapped for KS).") 

bal.table.evang.latex<-latex(bal.table.evang,file='bal_table_evang.tex',rowlabel='',collabel.just=rep('r',6),col.just = rep('r',6), caption = "Covariate Balance for Evangelical Treatment", insert.bottom=bottom.note,rgroup = c('Comuna','Region','Religion','Politics','Demographics'), n.rgroup = c(1,3,5,3,3), booktabs = F, ctable = T, where = "htp",na.blank=T)

# ================
# Appendix Table 6
# ================

pinochetprime.evangcand.noevang.right.screen<-with(survey[!survey$real.cand&survey$treat.evang&!survey$evangelical&survey$tendency=='Rightist',],lm(evang.cand.vote~treat.pinochet*screener))
pinochetprime.evangcand.noevang.centerleft.screen<-with(survey[!survey$real.cand&survey$treat.evang&!survey$evangelical&survey$tendency %in% c('Centrist','Leftist'),],lm(evang.cand.vote~treat.pinochet*screener))
pinochetprime.evangcand.evang.screen<-with(survey[!survey$real.cand&survey$treat.evang&survey$evangelical,],lm(evang.cand.vote~treat.pinochet*screener))

se.pinochetprime.evangcand.noevang.right.screen<-paste('(',round(coef(summary(pinochetprime.evangcand.noevang.right.screen))[,2],2),')',sep='')
se.pinochetprime.evangcand.noevang.centerleft.screen<-paste('(',round(coef(summary(pinochetprime.evangcand.noevang.centerleft.screen))[,2],2),')',sep='')
se.pinochetprime.evangcand.evang.screen<-paste('(',round(coef(summary(pinochetprime.evangcand.evang.screen))[,2],2),')',sep='')

spacer.pinochetprime.evangcand.noevang.right.screen<- ifelse(round(coef(summary(pinochetprime.evangcand.noevang.right.screen))[,1],2)<0,'','\\ ')
spacer.pinochetprime.evangcand.noevang.centerleft.screen<- ifelse(round(coef(summary(pinochetprime.evangcand.noevang.centerleft.screen))[,1],2)<0,'','\\ ')
spacer.pinochetprime.evangcand.evang.screen<- ifelse(round(coef(summary(pinochetprime.evangcand.evang.screen))[,1],2)<0,'','\\ ')

est.pinochetprime.evangcand.noevang.right.screen<-paste(spacer.pinochetprime.evangcand.noevang.right.screen, round(coef(summary(pinochetprime.evangcand.noevang.right.screen))[,1],2),sep='')
est.pinochetprime.evangcand.noevang.centerleft.screen<-paste(spacer.pinochetprime.evangcand.noevang.centerleft.screen, round(coef(summary(pinochetprime.evangcand.noevang.centerleft.screen))[,1],2),sep='')
est.pinochetprime.evangcand.evang.screen<-paste(spacer.pinochetprime.evangcand.evang.screen, round(coef(summary(pinochetprime.evangcand.evang.screen))[,1],2),sep='')

vote.pinochetprime.evangcand.noevang.right.screen<-c(as.vector(rbind(est.pinochetprime.evangcand.noevang.right.screen, se.pinochetprime.evangcand.noevang.right.screen)), pinochetprime.evangcand.noevang.right.screen$rank+ pinochetprime.evangcand.noevang.right.screen$df.residual)
vote.pinochetprime.evangcand.noevang.centerleft.screen<-c(as.vector(rbind(est.pinochetprime.evangcand.noevang.centerleft.screen, se.pinochetprime.evangcand.noevang.centerleft.screen)), pinochetprime.evangcand.noevang.centerleft.screen$rank+ pinochetprime.evangcand.noevang.centerleft.screen$df.residual)
vote.pinochetprime.evangcand.evang.screen<-c(as.vector(rbind(est.pinochetprime.evangcand.evang.screen, se.pinochetprime.evangcand.evang.screen)), pinochetprime.evangcand.evang.screen$rank+ pinochetprime.evangcand.evang.screen$df.residual)

results.table.pinochetprime.evangcand.screen<-data.frame(vote.pinochetprime.evangcand.noevang.right.screen, vote.pinochetprime.evangcand.noevang.centerleft.screen,
vote.pinochetprime.evangcand.evang.screen)
bottom.note<-paste("NOTE: Entries are OLS regression coefficients with estimated standard errors in parentheses.")

results.table.pinochetprime.evangcand.screen.latex<-latex(results.table.pinochetprime.evangcand.screen,file='results_table_pinochetprime_evangcand_screen.tex',collabel.just=rep('l',8),col.just = rep('l',8),rowname=c('Intercept','','Pinochet Prime','','1 Screener','','2 Screeners','','Pinochet $\\times$ 1 Screener','','Pinochet $\\times$ 2 Screeners','','\\textit{N}'),rowlabel='',colheads=c('Right-Wing\nNon-Evangelicals','Center-Left\nNon-Evangelicals','\nEvangelicals'),extracolsize='normalsize',caption = 'Effect of Pinochet Stereotypes on Vote Intention for an Evangelical Candidate, by Screener Passage',insert.bottom=bottom.note,booktabs = F, ctable = T, where = "htp",cgroup='Subgroup',n.cgroup=3,landscape=F)

# ================
# Appendix Table 7
# ================

evangcand.nopinochetprime.noevang.right.screen<-with(survey[!survey$real.cand&!survey$treat.pinochet&!survey$evangelical&survey$tendency=='Rightist',],lm(evang.cand.vote~treat.evang*screener))
evangcand.nopinochetprime.noevang.centerleft.screen<-with(survey[!survey$real.cand&!survey$treat.pinochet&!survey$evangelical&survey$tendency %in% c('Centrist','Leftist'),],lm(evang.cand.vote~treat.evang*screener))
evangcand.nopinochetprime.evang.screen<-with(survey[!survey$real.cand&!survey$treat.pinochet&survey$evangelical,],lm(evang.cand.vote~treat.evang*screener))

se.evangcand.nopinochetprime.noevang.right.screen<-paste('(',round(coef(summary(evangcand.nopinochetprime.noevang.right.screen))[,2],2),')',sep='')
se.evangcand.nopinochetprime.noevang.centerleft.screen<-paste('(',round(coef(summary(evangcand.nopinochetprime.noevang.centerleft.screen))[,2],2),')',sep='')
se.evangcand.nopinochetprime.evang.screen<-paste('(',round(coef(summary(evangcand.nopinochetprime.evang.screen))[,2],2),')',sep='')

spacer.evangcand.nopinochetprime.noevang.right.screen<- ifelse(round(coef(summary(evangcand.nopinochetprime.noevang.right.screen))[,1],2)<0,'','\\ ')
spacer.evangcand.nopinochetprime.noevang.centerleft.screen<- ifelse(round(coef(summary(evangcand.nopinochetprime.noevang.centerleft.screen))[,1],2)<0,'','\\ ')
spacer.evangcand.nopinochetprime.evang.screen<- ifelse(round(coef(summary(evangcand.nopinochetprime.evang.screen))[,1],2)<0,'','\\ ')

est.evangcand.nopinochetprime.noevang.right.screen<-paste(spacer.evangcand.nopinochetprime.noevang.right.screen, round(coef(summary(evangcand.nopinochetprime.noevang.right.screen))[,1],2),sep='')
est.evangcand.nopinochetprime.noevang.centerleft.screen<-paste(spacer.evangcand.nopinochetprime.noevang.centerleft.screen, round(coef(summary(evangcand.nopinochetprime.noevang.centerleft.screen))[,1],2),sep='')
est.evangcand.nopinochetprime.evang.screen<-paste(spacer.evangcand.nopinochetprime.evang.screen, round(coef(summary(evangcand.nopinochetprime.evang.screen))[,1],2),sep='')

vote.evangcand.nopinochetprime.noevang.right.screen<-c(as.vector(rbind(est.evangcand.nopinochetprime.noevang.right.screen, se.evangcand.nopinochetprime.noevang.right.screen)),evangcand.nopinochetprime.noevang.right.screen$rank+ evangcand.nopinochetprime.noevang.right.screen$df.residual)
vote.evangcand.nopinochetprime.noevang.centerleft.screen<-c(as.vector(rbind(est.evangcand.nopinochetprime.noevang.centerleft.screen, se.evangcand.nopinochetprime.noevang.centerleft.screen)),evangcand.nopinochetprime.noevang.centerleft.screen$rank+ evangcand.nopinochetprime.noevang.centerleft.screen$df.residual)
vote.evangcand.nopinochetprime.evang.screen<-c(as.vector(rbind(est.evangcand.nopinochetprime.evang.screen, se.evangcand.nopinochetprime.evang.screen)),evangcand.nopinochetprime.evang.screen$rank+ evangcand.nopinochetprime.evang.screen$df.residual)

results.table.evangcand.nopinochetprime.screen<-data.frame(vote.evangcand.nopinochetprime.noevang.right.screen, vote.evangcand.nopinochetprime.noevang.centerleft.screen, vote.evangcand.nopinochetprime.evang.screen)
bottom.note<-paste("NOTE: Entries are OLS regression coefficients with estimated standard errors in parentheses.")

results.table.evangcand.nopinochetprime.screen.latex<-latex(results.table.evangcand.nopinochetprime.screen,file='results_table_evangcand_nopinochetprime_screen.tex',collabel.just=rep('l',8),col.just = rep('l',8),rowname=c('Intercept','','Evangelical Candidate','','1 Screener','','2 Screeners','','Evang. Cand. $\\times$ 1 Screener','','Evang. Cand. $\\times$ 2 Screeners','','\\textit{N}'),rowlabel='',colheads=c('Right-Wing\nNon-Evangelicals','Center-Left\nNon-Evangelicals','\nEvangelicals'),extracolsize='normalsize',caption = 'Effect of Candidate Evangelicalism on Vote Intention When Pinochet Stereotypes Are Not Primed, by Screener Passage',insert.bottom=bottom.note,booktabs = F, ctable = T, where = "htp",cgroup='Subgroup',n.cgroup=3,landscape=F)

# ================
# Appendix Table 9
# ================

pinochetprime.evangcand.real<-with(survey[survey$real.cand.comuna&survey$treat.evang,],lm(evang.cand.vote~real.cand*treat.pinochet))
evangcand.pinochetprime.real<-with(survey[survey$real.cand.comuna&survey$treat.pinochet,],lm(evang.cand.vote~real.cand*treat.evang))
evangcand.nopinochetprime.real<-with(survey[survey$real.cand.comuna&!survey$treat.pinochet,],lm(evang.cand.vote~real.cand*treat.evang))

se.pinochetprime.evangcand.real<-paste('(',round(coef(summary(pinochetprime.evangcand.real))[,2],2),')',sep='')
se.evangcand.pinochetprime.real<-paste('(',round(coef(summary(evangcand.pinochetprime.real))[,2],2),')',sep='')
se.evangcand.nopinochetprime.real<-paste('(',round(coef(summary(evangcand.nopinochetprime.real))[,2],2),')',sep='')

spacer.pinochetprime.evangcand.real<- ifelse(round(coef(summary(pinochetprime.evangcand.real))[,1],2)<0,'','\\ ')
spacer.evangcand.pinochetprime.real<- ifelse(round(coef(summary(evangcand.pinochetprime.real))[,1],2)<0,'','\\ ')
spacer.evangcand.nopinochetprime.real<- ifelse(round(coef(summary(evangcand.nopinochetprime.real))[,1],2)<0,'','\\ ')

est.pinochetprime.evangcand.real<-paste(spacer.pinochetprime.evangcand.real, round(coef(summary(pinochetprime.evangcand.real))[,1],2),sep='')
est.evangcand.pinochetprime.real<-paste(spacer.evangcand.pinochetprime.real, round(coef(summary(evangcand.pinochetprime.real))[,1],2),sep='')
est.evangcand.nopinochetprime.real<-paste(spacer.evangcand.nopinochetprime.real, round(coef(summary(evangcand.nopinochetprime.real))[,1],2),sep='')

vote.pinochetprime.evangcand.real<-c(as.vector(rbind(est.pinochetprime.evangcand.real, se.pinochetprime.evangcand.real)), '','','','',pinochetprime.evangcand.real$rank+ pinochetprime.evangcand.real$df.residual)
vote.evangcand.pinochetprime.real<-c(as.vector(rbind(est.evangcand.pinochetprime.real, se.evangcand.pinochetprime.real)),evangcand.pinochetprime.real$rank+ evangcand.pinochetprime.real$df.residual)
vote.evangcand.pinochetprime.real<-c(vote.evangcand.pinochetprime.real[1:4],'','','','', vote.evangcand.pinochetprime.real[5:9])
vote.evangcand.nopinochetprime.real<-c(as.vector(rbind(est.evangcand.nopinochetprime.real, se.evangcand.nopinochetprime.real)),evangcand.nopinochetprime.real$rank+ evangcand.nopinochetprime.real$df.residual)
vote.evangcand.nopinochetprime.real<-c(vote.evangcand.nopinochetprime.real[1:4],'','','','', vote.evangcand.nopinochetprime.real[5:9])

results.table.real<-data.frame(vote.pinochetprime.evangcand.real, vote.evangcand.pinochetprime.real, vote.evangcand.nopinochetprime.real)
bottom.note<-paste("NOTE: Entries are OLS regression coefficients with estimated standard errors in parentheses. Includes only respondents from congressional districts with evangelical candidates, as listed in Table \\ref{tab:evang.cands}.")

results.table.real.latex<-latex(results.table.real,file='results_table_real.tex',collabel.just=rep('l',8),col.just = rep('l',8),rowname=c('Intercept','','Real Candidate','','Pinochet Prime','','Real Cand. $\\times$ Pinochet','','Evangelical Candidate','','Real Cand. $\\times$ Evang. Cand.','','\\textit{N}'),rowlabel='',colheads=c('Evang. Cand.','Pinochet','$\\neg$Pinochet'),extracolsize='normalsize',caption = 'Treatment Effects on Vote Intention for Real vs. Fictional Candidates',insert.bottom=bottom.note,booktabs = F, ctable = T, where = "htp",cgroup='Conditional on:',n.cgroup=3,landscape=F)

# =================
# Appendix Table 10
# =================

pinochetprime.evangcand.noevang.right<-with(survey[!survey$real.cand&survey$treat.evang&!survey$evangelical&survey$tendency=='Rightist',],lm(evang.cand.vote~treat.pinochet))
pinochetprime.noevangcand.noevang.right<-with(survey[!survey$real.cand&!survey$treat.evang&!survey$evangelical&survey$tendency=='Rightist',],lm(evang.cand.vote~treat.pinochet))
evangcand.pinochetprime.noevang.right<-with(survey[!survey$real.cand&survey$treat.pinochet&!survey$evangelical&survey$tendency=='Rightist',],lm(evang.cand.vote~treat.evang))
evangcand.nopinochetprime.noevang.right<-with(survey[!survey$real.cand&!survey$treat.pinochet&!survey$evangelical&survey$tendency=='Rightist',],lm(evang.cand.vote~treat.evang))

noevang.table.right<-round(matrix(c(sum(coef(pinochetprime.evangcand.noevang.right)),
	coef(pinochetprime.evangcand.noevang.right)[1],
	coef(pinochetprime.evangcand.noevang.right)[2],
	sum(summary(pinochetprime.evangcand.noevang.right)$df[1:2]),
	NA,NA,coef(summary(pinochetprime.evangcand.noevang.right))[2,2],NA,
	sum(coef(pinochetprime.noevangcand.noevang.right)),
	coef(pinochetprime.noevangcand.noevang.right)[1],
	coef(pinochetprime.noevangcand.noevang.right)[2],
	sum(summary(pinochetprime.noevangcand.noevang.right)$df[1:2]),
	NA,NA,coef(summary(pinochetprime.noevangcand.noevang.right))[2,2],NA,
	coef(evangcand.pinochetprime.noevang.right)[2],
	coef(evangcand.nopinochetprime.noevang.right)[2],NA,NA,
	coef(summary(evangcand.pinochetprime.noevang.right))[2,2],
	coef(summary(evangcand.nopinochetprime.noevang.right))[2,2],NA,NA,
	NA,NA,NA,NA,
	sum(summary(evangcand.pinochetprime.noevang.right)$df[1:2]),
	sum(summary(evangcand.nopinochetprime.noevang.right)$df[1:2]),NA,NA
	),ncol=4,byrow=T),2)

noevang.table.right[c(2,4,6),]<-t(apply(noevang.table.right[c(2,4,6),],1,function(x) ifelse(is.na(x), NA, paste('(',x,')',sep=''))))
noevang.table.right<-apply(noevang.table.right,2,function(x) ifelse(is.na(x),'',x))

noevang.right.n<-sum(summary(pinochetprime.noevangcand.noevang.right)$df[1:2])+ sum(summary(pinochetprime.evangcand.noevang.right)$df[1:2])

noevang.table.right.latex<-latex(noevang.table.right,file='noevang_table_right.tex',caption='Effect of Pinochet Stereotypes and Candidate Evangelicalism on Vote Intention (Right-Wing Non-Evangelical Respondents)',rowlabel='',rowname=c('Mentioned','','Not Mentioned','','Difference','','','$N$'),colheads=c('Yes','No','Difference','$N$'), collabel.just=rep('c',4),col.just = rep('c',4),rgroup=c('Evangelicalism'),n.rgroup=8,cgroup=c('Prime Pinochet',''),n.cgroup=c(2,2), booktabs = F, ctable = T, where = "htp", insert.bottom='NOTE: Entries are mean vote intention and differences in vote intention, measured on a 1--7 scale, with estimated standard errors in parentheses.') 

# =================
# Appendix Table 11
# =================

pinochetprime.evangcand.noevang.centerleft<-with(survey[!survey$real.cand&survey$treat.evang&!survey$evangelical&survey$tendency %in% c('Centrist','Leftist'),],lm(evang.cand.vote~treat.pinochet))
pinochetprime.noevangcand.noevang.centerleft<-with(survey[!survey$real.cand&!survey$treat.evang&!survey$evangelical&survey$tendency %in% c('Centrist','Leftist'),],lm(evang.cand.vote~treat.pinochet))
evangcand.pinochetprime.noevang.centerleft<-with(survey[!survey$real.cand&survey$treat.pinochet&!survey$evangelical&survey$tendency %in% c('Centrist','Leftist'),],lm(evang.cand.vote~treat.evang))
evangcand.nopinochetprime.noevang.centerleft<-with(survey[!survey$real.cand&!survey$treat.pinochet&!survey$evangelical&survey$tendency %in% c('Centrist','Leftist'),],lm(evang.cand.vote~treat.evang))

noevang.table.centerleft<-round(matrix(c(sum(coef(pinochetprime.evangcand.noevang.centerleft)),
	coef(pinochetprime.evangcand.noevang.centerleft)[1],
	coef(pinochetprime.evangcand.noevang.centerleft)[2],
	sum(summary(pinochetprime.evangcand.noevang.centerleft)$df[1:2]),
	NA,NA,coef(summary(pinochetprime.evangcand.noevang.centerleft))[2,2],NA,
	sum(coef(pinochetprime.noevangcand.noevang.centerleft)),
	coef(pinochetprime.noevangcand.noevang.centerleft)[1],
	coef(pinochetprime.noevangcand.noevang.centerleft)[2],
	sum(summary(pinochetprime.noevangcand.noevang.centerleft)$df[1:2]),
	NA,NA,coef(summary(pinochetprime.noevangcand.noevang.centerleft))[2,2],NA,
	coef(evangcand.pinochetprime.noevang.centerleft)[2],
	coef(evangcand.nopinochetprime.noevang.centerleft)[2],NA,NA,
	coef(summary(evangcand.pinochetprime.noevang.centerleft))[2,2],
	coef(summary(evangcand.nopinochetprime.noevang.centerleft))[2,2],NA,NA,
	NA,NA,NA,NA,
	sum(summary(evangcand.pinochetprime.noevang.centerleft)$df[1:2]),
	sum(summary(evangcand.nopinochetprime.noevang.centerleft)$df[1:2]),NA,NA
	),ncol=4,byrow=T),2)


noevang.table.centerleft[c(2,4,6),]<-t(apply(noevang.table.centerleft[c(2,4,6),],1,function(x) ifelse(is.na(x), NA, paste('(',x,')',sep=''))))
noevang.table.centerleft<-apply(noevang.table.centerleft,2,function(x) ifelse(is.na(x),'',x))

noevang.centerleft.n<-sum(summary(pinochetprime.noevangcand.noevang.centerleft)$df[1:2])+ sum(summary(pinochetprime.evangcand.noevang.centerleft)$df[1:2])

noevang.table.centerleft.latex<-latex(noevang.table.centerleft,file='noevang_table_centerleft.tex',caption='Effect of Pinochet Stereotypes and Candidate Evangelicalism on Vote Intention (Centrist and Left-Wing Non-Evangelical Respondents)',rowlabel='',rowname=c('Mentioned','','Not Mentioned','','Difference','','','$N$'),colheads=c('Yes','No','Difference','$N$'), collabel.just=rep('c',4),col.just = rep('c',4),rgroup=c('Evangelicalism'),n.rgroup=8,cgroup=c('Prime Pinochet',''),n.cgroup=c(2,2), booktabs = F, ctable = T, where = "htp", insert.bottom='NOTE: Entries are mean vote intention and differences in vote intention, measured on a 1--7 scale, with estimated standard errors in parentheses.') 

# =================
# Appendix Table 12
# =================

pinochetprime.evangcand.evang<-with(survey[!survey$real.cand&survey$treat.evang&survey$evangelical,],lm(evang.cand.vote~treat.pinochet))
pinochetprime.noevangcand.evang<-with(survey[!survey$real.cand&!survey$treat.evang&survey$evangelical,],lm(evang.cand.vote~treat.pinochet))
evangcand.pinochetprime.evang<-with(survey[!survey$real.cand&survey$treat.pinochet&survey$evangelical,],lm(evang.cand.vote~treat.evang))
evangcand.nopinochetprime.evang<-with(survey[!survey$real.cand&!survey$treat.pinochet&survey$evangelical,],lm(evang.cand.vote~treat.evang))

evang.table.all<-round(matrix(c(sum(coef(pinochetprime.evangcand.evang)),
	coef(pinochetprime.evangcand.evang)[1],
	coef(pinochetprime.evangcand.evang)[2],
	sum(summary(pinochetprime.evangcand.evang)$df[1:2]),
	NA,NA,coef(summary(pinochetprime.evangcand.evang))[2,2],NA,
	sum(coef(pinochetprime.noevangcand.evang)),
	coef(pinochetprime.noevangcand.evang)[1],
	coef(pinochetprime.noevangcand.evang)[2],
	sum(summary(pinochetprime.noevangcand.evang)$df[1:2]),
	NA,NA,coef(summary(pinochetprime.noevangcand.evang))[2,2],NA,
	coef(evangcand.pinochetprime.evang)[2],
	coef(evangcand.nopinochetprime.evang)[2],NA,NA,
	coef(summary(evangcand.pinochetprime.evang))[2,2],
	coef(summary(evangcand.nopinochetprime.evang))[2,2],NA,NA,
	NA,NA,NA,NA,
	sum(summary(evangcand.pinochetprime.evang)$df[1:2]),
	sum(summary(evangcand.nopinochetprime.evang)$df[1:2]),NA,NA
	),ncol=4,byrow=T),2)

evang.table.all[c(2,4,6),]<-t(apply(evang.table.all[c(2,4,6),],1,function(x) ifelse(is.na(x), NA, paste('(',x,')',sep=''))))
evang.table.all<-apply(evang.table.all,2,function(x) ifelse(is.na(x),'',x))

evang.all.n<-sum(summary(pinochetprime.noevangcand.evang)$df[1:2])+ sum(summary(pinochetprime.evangcand.evang)$df[1:2])

evang.table.all.latex<-latex(evang.table.all,file='evang_table_all.tex',caption='Effect of Pinochet Stereotypes and Candidate Evangelicalism on Vote Intention (Evangelical Respondents)',rowlabel='',rowname=c('Mentioned','','Not Mentioned','','Difference','','','$N$'),colheads=c('Yes','No','Difference','$N$'), collabel.just=rep('c',4),col.just = rep('c',4),rgroup=c('Evangelicalism'),n.rgroup=8,cgroup=c('Prime Pinochet',''),n.cgroup=c(2,2), booktabs = F, ctable = T, where = "htp", insert.bottom='NOTE: Entries are mean vote intention and differences in vote intention, measured on a 1--7 scale, with estimated standard errors in parentheses.') 

# =======================================
# Appendix Table 13 and Appendix Figure 2
# =======================================

ideol.vals<-with(survey[!survey$real.cand,],seq(min(ideol,na.rm=T),max(ideol,na.rm=T),.01))

pinochetprime.evangcand.evang.interact<-with(survey[!survey$real.cand&survey$treat.evang&survey$evangelical,],lm(evang.cand.vote~treat.pinochet*ideol))
se.pinochetprime.evangcand.evang.interact<-paste('(',round(coef(summary(pinochetprime.evangcand.evang.interact))[,2],2),')',sep='')
spacer.pinochetprime.evangcand.evang.interact <- ifelse(round(coef(summary(pinochetprime.evangcand.evang.interact))[,1],2)<0,'','\\ ')
est.pinochetprime.evangcand.evang.interact <-paste(spacer.pinochetprime.evangcand.evang.interact, round(coef(summary(pinochetprime.evangcand.evang.interact))[,1],2),sep='')
results.table.interact.evang<-c(as.vector(rbind(est.pinochetprime.evangcand.evang.interact, se.pinochetprime.evangcand.evang.interact)), pinochetprime.evangcand.evang.interact$rank+ pinochetprime.evangcand.evang.interact$df.residual)
bottom.note<-paste("NOTE: Entries are OLS regression coefficients with estimated standard errors in parentheses. Ideology is scaled from 1--10; higher numbers are Right.")

results.table.interact.latex<-latex(results.table.interact.evang,file='results_table_interact_evang.tex',collabel.just=rep('l',1),col.just = rep('l',1),rowname=c('Intercept','','Pinochet Prime','','Ideology','','Pinochet Prime $\\times$ Ideology','','\\textit{N}'),rowlabel='',colheads=c(''),extracolsize='normalsize',caption = "Effect of Pinochet Stereotypes on Evangelicals' Vote Intention for an Evangelical Candidate, Conditional on Ideology",insert.bottom=bottom.note,booktabs = F, ctable = T, where = "htp",landscape=F)

point.est.evang<-coef(pinochetprime.evangcand.evang.interact)[2]+ coef(pinochetprime.evangcand.evang.interact)[4]*ideol.vals
std.err.evang<-sqrt(vcov(pinochetprime.evangcand.evang.interact)[2,2]+ideol.vals^2* vcov(pinochetprime.evangcand.evang.interact)[4,4]+2*ideol.vals* vcov(pinochetprime.evangcand.evang.interact)[2,4])

pdf('ideol_interact_evang.pdf',height=6.5,width=6.5)
plot(ideol.vals,point.est.evang,type='l',lwd=2,ylim=c(-3.5,3.5),xlab='Ideology',ylab='Conditional Effect of Pinochet Prime',main='')
abline(h=0,lty=2,lwd=2)
lines(ideol.vals,point.est.evang+qt(.975,pinochetprime.evangcand.evang.interact$df.residual)*std.err.evang,lty=3,lwd=2)
lines(ideol.vals,point.est.evang-qt(.975,pinochetprime.evangcand.evang.interact$df.residual)*std.err.evang,lty=3,lwd=2)
dev.off()

# =======================================
# Appendix Table 14 and Appendix Figure 3
# =======================================

ideol.vals<-with(survey[!survey$real.cand,],seq(min(ideol,na.rm=T),max(ideol,na.rm=T),.01))

pinochetprime.evangcand.noevang.interact<-with(survey[!survey$real.cand&survey$treat.evang&!survey$evangelical,],lm(evang.cand.vote~treat.pinochet*ideol))
se.pinochetprime.evangcand.noevang.interact<-paste('(',round(coef(summary(pinochetprime.evangcand.noevang.interact))[,2],2),')',sep='')
spacer.pinochetprime.evangcand.noevang.interact <- ifelse(round(coef(summary(pinochetprime.evangcand.noevang.interact))[,1],2)<0,'','\\ ')
est.pinochetprime.evangcand.noevang.interact <-paste(spacer.pinochetprime.evangcand.noevang.interact, round(coef(summary(pinochetprime.evangcand.noevang.interact))[,1],2),sep='')
results.table.interact<-c(as.vector(rbind(est.pinochetprime.evangcand.noevang.interact, se.pinochetprime.evangcand.noevang.interact)), pinochetprime.evangcand.noevang.interact$rank+ pinochetprime.evangcand.noevang.interact$df.residual)
bottom.note<-paste("NOTE: Entries are OLS regression coefficients with estimated standard errors in parentheses. Ideology is scaled from 1--10; higher numbers are Right.")

results.table.interact.latex<-latex(results.table.interact,file='results_table_interact.tex',collabel.just=rep('l',1),col.just = rep('l',1),rowname=c('Intercept','','Pinochet Prime','','Ideology','','Pinochet Prime $\\times$ Ideology','','\\textit{N}'),rowlabel='',colheads=c(''),extracolsize='normalsize',caption = "Effect of Pinochet Stereotypes on Non-Evangelicals' Vote Intention for an Evangelical Candidate, Conditional on Ideology",insert.bottom=bottom.note,booktabs = F, ctable = T, where = "htp",landscape=F)

point.est.noevang<-coef(pinochetprime.evangcand.noevang.interact)[2]+ coef(pinochetprime.evangcand.noevang.interact)[4]*ideol.vals
std.err.noevang<-sqrt(vcov(pinochetprime.evangcand.noevang.interact)[2,2]+ideol.vals^2* vcov(pinochetprime.evangcand.noevang.interact)[4,4]+2*ideol.vals* vcov(pinochetprime.evangcand.noevang.interact)[2,4])

pdf('ideol_interact_noevang.pdf',height=6.5,width=6.5)
par(mar=c(4,4,2,1)+.1)
plot(ideol.vals,point.est.noevang,type='l',lwd=2,ylim=c(-1.6,2.4),xlab='Ideology',ylab='Conditional Effect of Pinochet Prime',main='')
abline(h=0,lty=2,lwd=2)
lines(ideol.vals,point.est.noevang+qt(.975,pinochetprime.evangcand.noevang.interact$df.residual)*std.err.noevang,lty=3,lwd=2)
lines(ideol.vals,point.est.noevang-qt(.975,pinochetprime.evangcand.noevang.interact$df.residual)*std.err.noevang,lty=3,lwd=2)
dev.off()

# =======================================
# Appendix Table 15 and Appendix Figure 4
# =======================================

church.vals<-with(survey[!survey$real.cand,],seq(min(church.rev,na.rm=T),max(church.rev,na.rm=T),.01))

pinochetprime.evangcand.evang.interact2<-with(survey[!survey$real.cand&survey$treat.evang&survey$evangelical,],lm(evang.cand.vote~treat.pinochet*church.rev))
se.pinochetprime.evangcand.evang.interact2<-paste('(',round(coef(summary(pinochetprime.evangcand.evang.interact2))[,2],2),')',sep='')
spacer.pinochetprime.evangcand.evang.interact2 <- ifelse(round(coef(summary(pinochetprime.evangcand.evang.interact2))[,1],2)<0,'','\\ ')
est.pinochetprime.evangcand.evang.interact2 <-paste(spacer.pinochetprime.evangcand.evang.interact2, round(coef(summary(pinochetprime.evangcand.evang.interact2))[,1],2),sep='')
results.table.interact2<-c(as.vector(rbind(est.pinochetprime.evangcand.evang.interact2, se.pinochetprime.evangcand.evang.interact2)), pinochetprime.evangcand.evang.interact2$rank+ pinochetprime.evangcand.evang.interact2$df.residual)
bottom.note2<-paste("NOTE: Entries are OLS regression coefficients with estimated standard errors in parentheses. Church Attendance is scaled from 0 (never or almost never) to 4 (more than once a week).")
results.table.interact2.latex<-latex(results.table.interact2,file='results_table_interact_church.tex',collabel.just=rep('l',1),col.just = rep('l',1),rowname=c('Intercept','','Pinochet Prime','','Church Attendance','','Pinochet Prime $\\times$ Church Attendance','','\\textit{N}'),rowlabel='',colheads=c(''),extracolsize='normalsize',caption = "Effect of Pinochet Stereotypes on Evangelicals' Vote Intention for an Evangelical Candidate, Conditional on Church Attendance",insert.bottom=bottom.note2,booktabs = F, ctable = T, where = "htp",landscape=F)

point.est.evang2<-coef(pinochetprime.evangcand.evang.interact2)[2]+ coef(pinochetprime.evangcand.evang.interact2)[4]*church.vals
std.err.evang2<-sqrt(vcov(pinochetprime.evangcand.evang.interact2)[2,2]+church.vals^2* vcov(pinochetprime.evangcand.evang.interact2)[4,4]+2*church.vals* vcov(pinochetprime.evangcand.evang.interact2)[2,4])

pdf('church_interact_evang.pdf',height=6.5,width=6.5)
plot(church.vals,point.est.evang2,type='l',lwd=2,ylim=c(-3.5,3.5),xlab='Church Attendance',ylab='Conditional Effect of Pinochet Prime',main='')
abline(h=0,lty=2,lwd=2)
lines(church.vals,point.est.evang2+qt(.975,pinochetprime.evangcand.evang.interact2$df.residual)*std.err.evang2,lty=3,lwd=2)
lines(church.vals,point.est.evang2-qt(.975,pinochetprime.evangcand.evang.interact2$df.residual)*std.err.evang2,lty=3,lwd=2)
dev.off()

# =============================
# Numbers for Appendix Figure 5
# =============================

# NOTE: The total number who were assessed for eligibility, declined to participate, did not answer eligibility questions, or did not meet inclusion criteria cannot be calculated using this public version of the dataset, which contains data only on eligible participants.

sum(is.na(survey$treat.pinochet)|is.na(survey$treat.evang)) # Dropout pre-randomization
sum(!is.na(survey$treat.pinochet)&!is.na(survey$treat.evang)) # Randomized

sum(survey$treat.pinochet&survey$treat.evang,na.rm=T) # Assigned to Pinochet prime, evangelical candidate
sum(survey$treat.pinochet&survey$treat.evang&is.na(survey$evang.cand.vote),na.rm=T) # Did not answer vote question
sum(survey$treat.pinochet&survey$treat.evang&!is.na(survey$evang.cand.vote)&is.na(survey$evangelical),na.rm=T) # Dropped out before religion question
sum(survey$treat.pinochet&survey$treat.evang&!is.na(survey$evang.cand.vote)&!is.na(survey$evangelical)&!survey$evangelical&is.na(survey$ideol),na.rm=T) # Non-evangelical and did not answer ideology question
sum(survey$treat.pinochet&survey$treat.evang&!is.na(survey$evang.cand.vote)&!is.na(survey$evangelical)&!(!survey$evangelical&is.na(survey$ideol))&survey$real.cand,na.rm=T) # Real candidate version of the treatment
sum(survey$treat.pinochet&survey$treat.evang&!is.na(survey$evang.cand.vote)&!is.na(survey$evangelical)&!(!survey$evangelical&is.na(survey$ideol))&!survey$real.cand,na.rm=T) # Analyzed

sum(survey$treat.pinochet&!survey$treat.evang,na.rm=T) # Assigned to Pinochet prime, no evangelical candidate
sum(survey$treat.pinochet&!survey$treat.evang&is.na(survey$evang.cand.vote),na.rm=T) # Did not answer vote question
sum(survey$treat.pinochet&!survey$treat.evang&!is.na(survey$evang.cand.vote)&is.na(survey$evangelical),na.rm=T) # Dropped out before religion question
sum(survey$treat.pinochet&!survey$treat.evang&!is.na(survey$evang.cand.vote)&!is.na(survey$evangelical)&!survey$evangelical&is.na(survey$ideol),na.rm=T) # Non-evangelical and did not answer ideology question
sum(survey$treat.pinochet&!survey$treat.evang&!is.na(survey$evang.cand.vote)&!is.na(survey$evangelical)&!(!survey$evangelical&is.na(survey$ideol))&survey$real.cand,na.rm=T) # Real candidate version of the treatment
sum(survey$treat.pinochet&!survey$treat.evang&!is.na(survey$evang.cand.vote)&!is.na(survey$evangelical)&!(!survey$evangelical&is.na(survey$ideol))&!survey$real.cand,na.rm=T) # Analyzed

sum(!survey$treat.pinochet&survey$treat.evang,na.rm=T) # Assigned to no Pinochet prime, evangelical candidate
sum(!survey$treat.pinochet&survey$treat.evang&is.na(survey$evang.cand.vote),na.rm=T) # Did not answer vote question
sum(!survey$treat.pinochet&survey$treat.evang&!is.na(survey$evang.cand.vote)&is.na(survey$evangelical),na.rm=T) # Dropped out before religion question
sum(!survey$treat.pinochet&survey$treat.evang&!is.na(survey$evang.cand.vote)&!is.na(survey$evangelical)&!survey$evangelical&is.na(survey$ideol),na.rm=T) # Non-evangelical and did not answer ideology question
sum(!survey$treat.pinochet&survey$treat.evang&!is.na(survey$evang.cand.vote)&!is.na(survey$evangelical)&!(!survey$evangelical&is.na(survey$ideol))&survey$real.cand,na.rm=T) # Real candidate version of the treatment
sum(!survey$treat.pinochet&survey$treat.evang&!is.na(survey$evang.cand.vote)&!is.na(survey$evangelical)&!(!survey$evangelical&is.na(survey$ideol))&!survey$real.cand,na.rm=T) # Analyzed

sum(!survey$treat.pinochet&!survey$treat.evang,na.rm=T) # Assigned to no Pinochet prime, no evangelical candidate
sum(!survey$treat.pinochet&!survey$treat.evang&is.na(survey$evang.cand.vote),na.rm=T) # Did not answer vote question
sum(!survey$treat.pinochet&!survey$treat.evang&!is.na(survey$evang.cand.vote)&is.na(survey$evangelical),na.rm=T) # Dropped out before religion question
sum(!survey$treat.pinochet&!survey$treat.evang&!is.na(survey$evang.cand.vote)&!is.na(survey$evangelical)&!survey$evangelical&is.na(survey$ideol),na.rm=T) # Non-evangelical and did not answer ideology question
sum(!survey$treat.pinochet&!survey$treat.evang&!is.na(survey$evang.cand.vote)&!is.na(survey$evangelical)&!(!survey$evangelical&is.na(survey$ideol))&survey$real.cand,na.rm=T) # Real candidate version of the treatment
sum(!survey$treat.pinochet&!survey$treat.evang&!is.na(survey$evang.cand.vote)&!is.na(survey$evangelical)&!(!survey$evangelical&is.na(survey$ideol))&!survey$real.cand,na.rm=T) # Analyzed

# =========================
# Appendix Tables 16 and 17
# =========================

survey$predropout<-is.na(survey$treat.pinochet)|is.na(survey$treat.evang)
survey$postdropout<-!is.na(survey$treat.pinochet)&!is.na(survey$treat.evang)&is.na(survey$evang.cand.vote)

mod.predropout.age<-glm(survey$predropout~survey$age,family=binomial(link=logit))
mod.predropout.interest<-glm(survey$predropout~survey$interest,family=binomial(link=logit))
mod.predropout.ideol<-glm(survey$predropout~survey$ideol,family=binomial(link=logit))
mod.predropout.all<-glm(survey$predropout~survey$age+survey$interest+survey$ideol,family=binomial(link=logit))

mod.postdropout.age<-glm(survey$postdropout~survey$age,family=binomial(link=logit))
mod.postdropout.interest<-glm(survey$postdropout~survey$interest,family=binomial(link=logit))
mod.postdropout.ideol<-glm(survey$postdropout~survey$ideol,family=binomial(link=logit))
mod.postdropout.pinochet<-glm(survey$postdropout~survey$treat.pinochet,family=binomial(link=logit))
mod.postdropout.evang<-glm(survey$postdropout~survey$treat.evang,family=binomial(link=logit))
mod.postdropout.all<-glm(survey$postdropout~survey$age+survey$interest+survey$ideol+survey$treat.pinochet+survey$treat.evang,family=binomial(link=logit))

se.predropout.age<-paste('(',round(coef(summary(mod.predropout.age))[,2],2),')',sep='')
se.predropout.interest<-paste('(',round(coef(summary(mod.predropout.interest))[,2],2),')',sep='')
se.predropout.ideol<-paste('(',round(coef(summary(mod.predropout.ideol))[,2],2),')',sep='')
se.predropout.all<-paste('(',round(coef(summary(mod.predropout.all))[,2],2),')',sep='')

se.postdropout.age<-paste('(',round(coef(summary(mod.postdropout.age))[,2],2),')',sep='')
se.postdropout.interest<-paste('(',round(coef(summary(mod.postdropout.interest))[,2],2),')',sep='')
se.postdropout.ideol<-paste('(',round(coef(summary(mod.postdropout.ideol))[,2],2),')',sep='')
se.postdropout.pinochet<-paste('(',round(coef(summary(mod.postdropout.pinochet))[,2],2),')',sep='')
se.postdropout.evang<-paste('(',round(coef(summary(mod.postdropout.evang))[,2],2),')',sep='')
se.postdropout.all<-paste('(',round(coef(summary(mod.postdropout.all))[,2],2),')',sep='')

spacer.predropout.age<-ifelse(round(coef(summary(mod.predropout.age))[,1],2)<0,'','\\ ')
spacer.predropout.interest<-ifelse(round(coef(summary(mod.predropout.interest))[,1],2)<0,'','\\ ')
spacer.predropout.ideol<-ifelse(round(coef(summary(mod.predropout.ideol))[,1],2)<0,'','\\ ')
spacer.predropout.all<-ifelse(round(coef(summary(mod.predropout.all))[,1],2)<0,'','\\ ')

spacer.postdropout.age<-ifelse(round(coef(summary(mod.postdropout.age))[,1],2)<0,'','\\ ')
spacer.postdropout.interest<-ifelse(round(coef(summary(mod.postdropout.interest))[,1],2)<0,'','\\ ')
spacer.postdropout.ideol<-ifelse(round(coef(summary(mod.postdropout.ideol))[,1],2)<0,'','\\ ')
spacer.postdropout.pinochet<-ifelse(round(coef(summary(mod.postdropout.pinochet))[,1],2)<0,'','\\ ')
spacer.postdropout.evang<-ifelse(round(coef(summary(mod.postdropout.evang))[,1],2)<0,'','\\ ')
spacer.postdropout.all<-ifelse(round(coef(summary(mod.postdropout.all))[,1],2)<0,'','\\ ')

est.predropout.age<-paste(spacer.predropout.age,round(coef(summary(mod.predropout.age))[,1],2),sep='')
est.predropout.interest<-paste(spacer.predropout.interest,round(coef(summary(mod.predropout.interest))[,1],2),sep='')
est.predropout.ideol<-paste(spacer.predropout.ideol,round(coef(summary(mod.predropout.ideol))[,1],2),sep='')
est.predropout.all<-paste(spacer.predropout.all,round(coef(summary(mod.predropout.all))[,1],2),sep='')

est.postdropout.age<-paste(spacer.postdropout.age,round(coef(summary(mod.postdropout.age))[,1],2),sep='')
est.postdropout.interest<-paste(spacer.postdropout.interest,round(coef(summary(mod.postdropout.interest))[,1],2),sep='')
est.postdropout.ideol<-paste(spacer.postdropout.ideol,round(coef(summary(mod.postdropout.ideol))[,1],2),sep='')
est.postdropout.pinochet<-paste(spacer.postdropout.pinochet,round(coef(summary(mod.postdropout.pinochet))[,1],2),sep='')
est.postdropout.evang<-paste(spacer.postdropout.evang,round(coef(summary(mod.postdropout.evang))[,1],2),sep='')
est.postdropout.all<-paste(spacer.postdropout.all,round(coef(summary(mod.postdropout.all))[,1],2),sep='')

predropout.age<-c(as.vector(rbind(est.predropout.age, se.predropout.age)), mod.predropout.age$rank+mod.predropout.age$df.residual)
predropout.interest<-c(as.vector(rbind(est.predropout.interest, se.predropout.interest)),mod.predropout.interest$rank+mod.predropout.interest$df.residual)
predropout.ideol<-c(as.vector(rbind(est.predropout.ideol, se.predropout.ideol)), mod.predropout.ideol$rank+mod.predropout.ideol$df.residual)
predropout.all<-c(as.vector(rbind(est.predropout.all, se.predropout.all)),mod.predropout.all$rank+mod.predropout.all$df.residual)

predropout.age<-c(predropout.age[1:4],'','','','',predropout.age[5])
predropout.interest<-c(predropout.age[1:2],'','', predropout.interest[3:4], '','', predropout.interest[5])
predropout.ideol<-c(predropout.ideol[1:2],'','','','',predropout.ideol[3:5])

predropout.table<-data.frame(predropout.age, predropout.interest, predropout.ideol, predropout.all)

postdropout.age<-c(as.vector(rbind(est.postdropout.age, se.postdropout.age)), mod.postdropout.age$rank+mod.postdropout.age$df.residual)
postdropout.interest<-c(as.vector(rbind(est.postdropout.interest, se.postdropout.interest)),mod.postdropout.interest$rank+mod.postdropout.interest$df.residual)
postdropout.ideol<-c(as.vector(rbind(est.postdropout.ideol, se.postdropout.ideol)), mod.postdropout.ideol$rank+mod.postdropout.ideol$df.residual)
postdropout.pinochet<-c(as.vector(rbind(est.postdropout.pinochet, se.postdropout.pinochet)), mod.postdropout.pinochet$rank+mod.postdropout.pinochet$df.residual)
postdropout.evang<-c(as.vector(rbind(est.postdropout.evang, se.postdropout.evang)), mod.postdropout.evang$rank+mod.postdropout.evang$df.residual)
postdropout.all<-c(as.vector(rbind(est.postdropout.all, se.postdropout.all)),mod.postdropout.all$rank+mod.postdropout.all$df.residual)

postdropout.age<-c(postdropout.age[1:4],'','','','','','','','',postdropout.age[5])
postdropout.interest<-c(postdropout.age[1:2],'','', postdropout.interest[3:4], '','','','','','', postdropout.interest[5])
postdropout.ideol<-c(postdropout.ideol[1:2],'','','','',postdropout.ideol[3:4],'','','','',postdropout.ideol[5])
postdropout.pinochet<-c(postdropout.pinochet[1:2],'','','','','','',postdropout.pinochet[3:4],'','',postdropout.pinochet[5])
postdropout.evang<-c(postdropout.evang[1:2],'','','','','','','','',postdropout.evang[3:5])

postdropout.table<-data.frame(postdropout.age, postdropout.interest, postdropout.ideol, postdropout.pinochet, postdropout.evang, postdropout.all)

bottom.note.dropout<-paste('Entries are logistic regression coefficients with estimated standard errors in parentheses.')

predropout.table.latex<-latex(predropout.table,file='predropout_table.tex',collabel.just=rep('l',4),col.just = rep('l',4),rowname=c('Intercept','','Age','','Campaign Interest','','Ideology','','\\textit{N}'),rowlabel='',colheads=rep('',4),extracolsize='normalsize',caption = "Predictors of Pre-Treatment Attrition",insert.bottom=bottom.note.dropout,booktabs = F, ctable = T, where = "htp",landscape=F)

postdropout.table.latex<-latex(postdropout.table,file='postdropout_table.tex',collabel.just=rep('l',6),col.just = rep('l',6),rowname=c('Intercept','','Age','','Campaign Interest','','Ideology','','Pinochet Treatment','','Evangelical Treatment','','\\textit{N}'),rowlabel='',colheads=rep('',6),extracolsize='normalsize',caption = "Predictors of Post-Treatment Attrition",insert.bottom=bottom.note.dropout,booktabs = F, ctable = T, where = "htp",landscape=F)

# =====================
# Main Article Figure 1
# =====================

diff<-c(
	coef(pinochetprime.evangcand.evang)[2],
	coef(evangcand.nopinochetprime.evang)[2],
	coef(pinochetprime.evangcand.noevang.right)[2],
	coef(evangcand.nopinochetprime.noevang.right)[2],
	coef(pinochetprime.evangcand.noevang.centerleft)[2],
	coef(evangcand.nopinochetprime.noevang.centerleft)[2])
se<-c(
	sqrt(vcov(pinochetprime.evangcand.evang)[2,2]),
	sqrt(vcov(evangcand.nopinochetprime.evang)[2,2]),
	sqrt(vcov(pinochetprime.evangcand.noevang.right)[2,2]),
	sqrt(vcov(evangcand.nopinochetprime.noevang.right)[2,2]),
	sqrt(vcov(pinochetprime.evangcand.noevang.centerleft)[2,2]),
	sqrt(vcov(evangcand.nopinochetprime.noevang.centerleft)[2,2]))
df<-c(
	pinochetprime.evangcand.evang$df.residual,
	evangcand.nopinochetprime.evang$df.residual,
	pinochetprime.evangcand.noevang.right$df.residual,
	evangcand.nopinochetprime.noevang.right$df.residual,
	pinochetprime.evangcand.noevang.centerleft$df.residual,
	evangcand.nopinochetprime.noevang.centerleft$df.residual)

lwd<-2

pdf(file='effect_plot.pdf',width=6,height=6)
par(mar=c(2,4,1,1)+.01)
plot(diff[c(1,3,5)], c(8,5,2), type='p',axes=F,frame.plot=T,xlab='',ylab='',pch=19,xlim=c(-1.5,3),ylim=c(0,9),main='',sub='')
Axis(side=1,at=seq(-1.5,3,.5),labels=seq(-1.5,3,.5))
Axis(side=2,at=c(7.5,4.5,1.5),labels=c(paste('Evangelicals\n(N = ',evang.all.n,')',sep=''),paste('Right-Wing\nNon-Evangelicals\n(N = ', noevang.right.n,')',sep=''),paste('Center-Left\nNon-Evangelicals\n(N = ',noevang.centerleft.n,')',sep='')),tick=F)
segments(diff-qt(.975,df)*se,c(8,7,5,4,2,1),diff+qt(.975,df)*se,c(8,7,5,4,2,1),lwd=lwd)
points(diff[c(2,4,6)], c(7,4,1), pch=25,bg='white')
abline(v=0,lwd=lwd,lty=2)

legend('bottomright',pch=c(19,25),legend=c('Pinochet | Evangelical','Evangelical | ¬Pinochet'),title='Conditional Effect')

dev.off()

# ======================================
# Results Mentioned in Main Article Text
# ======================================

# "In the 2012 AmericasBarometer survey, Chilean evangelicals had a significantly higher level of confidence in "the evangelical church"--which does not exist as a unified entity--than Catholics had in the Catholic Church (5.7 versus 5.1 on a 1-7 scale; p < 0.001)."

# Note: Load the 2012 AmericasBarometer Chile survey if not already loaded when replicating Appendix Table 2.

t.test(unclass(lapop12$b20[unclass(lapop12$q3c)==1]), unclass(lapop12$b20a[unclass(lapop12$q3c)==2| unclass(lapop12$q3c)==5]))

